﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditConcern : AdminPageBase
    {
        private ConcernInfo _concernInfo = new ConcernInfo();
        private readonly ConcernController _concernController = new ConcernController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ConcernId"]))
                _concernInfo = _concernController.GetConcern(int.Parse(Request.QueryString["ConcernId"]));

            if (Page.IsPostBack) return;

            LoadContinent();
            LoadAddressDefault();
            LoadStatus();
            LoadUser();
            LoadPartner();
            LoadConcernType();
            if (_concernInfo.ConcernId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();
            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListConcern.aspx?xml=Concern");
        }

        private void SaveData()
        {
            _concernInfo.ConcernFullName = txtConcernFullName.Text;
            _concernInfo.ConcernSortName = txtConcernSortName.Text;
            _concernInfo.ConcernEnglishName = txtConcernEnglishName.Text;
            _concernInfo.Status= int.Parse(ddlStatus.SelectedValue); 

            _concernInfo.ContinentId = int.Parse(ddlContinent.SelectedValue);
            _concernInfo.NationalId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlNational"]);
            _concernInfo.CityId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlCity"]);
            _concernInfo.DistrictId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _concernInfo.WardId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlWard"]);
            _concernInfo.Address = txtAddress.Text;

            _concernInfo.SortDescription = txtSortDescription.Text;
            _concernInfo.DetailDescription = txtContent.Text;
            _concernInfo.RepresentName = txtRepresentName.Text;
            _concernInfo.Phone = txtPhone.Text;
            _concernInfo.Hotline = txtHotline.Text;
            _concernInfo.Fax = txtFax.Text;
            _concernInfo.Email = txtEmail.Text;
            _concernInfo.Website = txtWebsite.Text;
            _concernInfo.Regency = txtRegency.Text;
            _concernInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _concernInfo.ConcernType = int.Parse(ddlConcernType.SelectedValue);


            if (_concernInfo.ConcernId == -1)
            {
                _concernInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _concernInfo.CreatedDate = DateTime.Now;
                _concernController.InsertConcern(_concernInfo);
            }
            else
            {
                _concernInfo.UpdatedBy = UserId;
                _concernInfo.UpdatedDate = DateTime.Now;
                _concernController.UpdateConcern(_concernInfo);
            }

            Response.Redirect("/Pages/ListConcern.aspx?xml=Concern");
        }

        private void RenderForm()
        {
            txtConcernFullName.Text=_concernInfo.ConcernFullName;
            txtConcernSortName.Text=_concernInfo.ConcernSortName;
            txtConcernEnglishName.Text=_concernInfo.ConcernEnglishName;

            txtSortDescription.Text=_concernInfo.SortDescription;
            txtContent.Text=_concernInfo.DetailDescription;
            txtRepresentName.Text=_concernInfo.RepresentName;
            txtPhone.Text=_concernInfo.Phone;
            txtHotline.Text=_concernInfo.Hotline;
            txtFax.Text=_concernInfo.Fax;
            txtEmail.Text=_concernInfo.Email;
            txtWebsite.Text=_concernInfo.Website;
            txtAddress.Text=_concernInfo.Address;

            ddlNational.SelectedValue = _concernInfo.NationalId.ToString();
            txtRegency.Text = _concernInfo.Regency;
            ddlCity.SelectedValue = _concernInfo.CityId.ToString();
            ddlWard.SelectedValue = _concernInfo.WardId.ToString();
            ddlDistrict.SelectedValue = _concernInfo.DistrictId.ToString();
            ddlContinent.SelectedValue = _concernInfo.ContinentId.ToString();
            ddlStatus.SelectedValue = _concernInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _concernInfo.CreatedBy.ToString();
            ddlPartner.SelectedValue = _concernInfo.PartnerId.ToString();
            ddlConcernType.SelectedValue = _concernInfo.ConcernType.ToString();
        }
        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadPartner()
        {
            ddlPartner.DataSource = new PartnerController().ListPartner()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.PartnerName).ToList();
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn nhà cung cấp ---", "-1"));
        }
        private void LoadConcernType()
        {
            ddlConcernType.Items.Insert(0, new ListItem("--- Chọn loại ---", "-1"));
            ddlConcernType.Items.Insert(1, new ListItem("Hãng hàng không", "1"));
            ddlConcernType.Items.Insert(2, new ListItem("Hãng tàu hỏa", "2"));
            ddlConcernType.Items.Insert(2, new ListItem("Hãng xe", "3"));
        }
        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_concernInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_concernInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_concernInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_concernInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}